package net.helix.core;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;

/**
 * A vertex class for Mesh objects. Also contains vertex normal
 * data (vx, vy, vz, vcount) for calculating normals in smooth
 * shading (when an imported model does not contain data for it).
 * @author Runouw
 *
 */
public class Vertex {
	private float x, y, z;
	private float vx = 0, vy = 0, vz = 0;
	private int vCount = 0;
	public Vertex(float x, float y, float z){
		this.x = x;
		this.y = y;
		this.z = z;
	}
	public void set(float x, float y, float z){
		this.x = x;
		this.y = y;
		this.z = z;
		updateBuffer = true;
	}
	public float getX(){
		return x;
	}
	public float getY(){
		return y;
	}
	public float getZ(){
		return z;
	}
	public void resetVertexNormals(){
		vx = 0;
		vy = 0;
		vz = 0;
		
		vCount = 0;
	}
	public void addVertexNormals(float x, float y, float z){
		vx += x;
		vy += y;
		vz += z;
		
		vCount++;
	}
	public float getVX(){
		if(vCount > 0){
			return vx/(float)vCount;
		}else{
			return 0;
		}
	}
	public float getVY(){
		if(vCount > 0){
			return vy/(float)vCount;
		}else{
			return 0;
		}
	}
	public float getVZ(){
		if(vCount > 0){
			return vz/(float)vCount;
		}else{
			return 0;
		}
	}
	private FloatBuffer buff = null;
	private boolean updateBuffer = true;
	public FloatBuffer getAsFloatBuffer(){
		if(buff == null){
			buff = BufferUtils.createFloatBuffer(4);
		}
		if(updateBuffer){
			buff.put(x);
			buff.put(y);
			buff.put(z);
			buff.put(1);
			buff.flip();
		}
		updateBuffer = false;
		return buff;
	}
}
